package com.exam.serviceimpl;

import com.exam.entity.FillQuestion;
import com.exam.entity.JudgeQuestion;
import com.exam.entity.MultiQuestion;
import com.exam.entity.WrongQuestion;
import com.exam.mapper.FillQuestionRepository;
import com.exam.mapper.JudgeQuestionRepository;
import com.exam.mapper.MultiQuestionRepository;
import com.exam.service.AutoGradingService;
import com.exam.service.WrongQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class AutoGradingServiceImpl implements AutoGradingService {

    @Autowired
    private FillQuestionRepository fillQuestionRepository;

    @Autowired
    private MultiQuestionRepository multiQuestionRepository;

    @Autowired
    private JudgeQuestionRepository judgeQuestionRepository;

    @Autowired
    private WrongQuestionService wrongQuestionService;
//FillQuestionRepository：用于操作填空题的数据库。
//MultiQuestionRepository：用于操作选择题的数据库。
//JudgeQuestionRepository：用于操作判断题的数据库。
//WrongQuestionService：用于处理错题记录的服务。
    @Override
    public Map<String, Object> gradeObjectiveQuestions(Map<String, String> userAnswers) {
        int correctCount = 0;
        int wrongCount = 0;

        for (Map.Entry<String, String> entry : userAnswers.entrySet()) {
            String questionId = entry.getKey();
            String userAnswer = entry.getValue();

            if (questionId.startsWith("F-")) { // 填空题
                Long id = Long.parseLong(questionId.substring(2));
                FillQuestion question = fillQuestionRepository.findById(id).orElse(null);
                if (question != null && question.getAnswer().equals(userAnswer)) {
                    correctCount++;
                } else {
                    saveWrongQuestion(question, "填空题", userAnswer);
                    wrongCount++;
                }
            } else if (questionId.startsWith("M-")) { // 选择题
                Long id = Long.parseLong(questionId.substring(2));
                MultiQuestion question = multiQuestionRepository.findById(id).orElse(null);
                if (question != null && question.getAnswer().equals(userAnswer)) {
                    correctCount++;
                } else {
                    saveWrongQuestion(question, "选择题", userAnswer);
                    wrongCount++;
                }
            } else if (questionId.startsWith("J-")) { // 判断题
                Long id = Long.parseLong(questionId.substring(2));
                JudgeQuestion question = judgeQuestionRepository.findById(id).orElse(null);
                if (question != null && question.getAnswer().equals(userAnswer)) {
                    correctCount++;
                } else {
                    saveWrongQuestion(question, "判断题", userAnswer);
                    wrongCount++;
                }
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("correctCount", correctCount);
        result.put("wrongCount", wrongCount);
        return result;
    }
//自动批改的主要方法，接收用户提交的答案，并返回批改结果。它遍历用户答案，根据题目类型（填空题、选择题、判断题）分别调用相应的数据库查询方法获取题目信息，
// 然后与用户答案进行比较，统计正确和错误的数量，并将错题保存到错题库中。
    private void saveWrongQuestion(Object question, String questionType, String userAnswer) {
        WrongQuestion wrongQuestion = new WrongQuestion();
        wrongQuestion.setQuestionType(questionType);
        wrongQuestion.setUserAnswer(userAnswer);

        if (question instanceof FillQuestion) {
            FillQuestion fillQuestion = (FillQuestion) question;
            wrongQuestion.setQuestionId("F-" + fillQuestion.getId());
            wrongQuestion.setCorrectAnswer(fillQuestion.getAnswer());
            wrongQuestion.setCourse(fillQuestion.getSubject());
            wrongQuestion.setExplanation(fillQuestion.getExplanation());
        } else if (question instanceof MultiQuestion) {
            MultiQuestion multiQuestion = (MultiQuestion) question;
            wrongQuestion.setQuestionId("M-" + multiQuestion.getId());
            wrongQuestion.setCorrectAnswer(multiQuestion.getAnswer());
            wrongQuestion.setCourse(multiQuestion.getSubject());
            wrongQuestion.setExplanation(multiQuestion.getExplanation());
        } else if (question instanceof JudgeQuestion) {
            JudgeQuestion judgeQuestion = (JudgeQuestion) question;
            wrongQuestion.setQuestionId("J-" + judgeQuestion.getId());
            wrongQuestion.setCorrectAnswer(judgeQuestion.getAnswer());
            wrongQuestion.setCourse(judgeQuestion.getSubject());
            wrongQuestion.setExplanation(judgeQuestion.getExplanation());
        }

        wrongQuestionService.addWrongQuestion(wrongQuestion);
    }
}